#include "FilterDialog.h"
#include <QLabel>
#include <QGridLayout>
#include <QPushButton>
#include <QMessageBox>

void FilterDialog::showDialog(){
    /// Sets the name
    this->setWindowTitle(filter->name());
    
    /// Instantiate the window
    {
        QGridLayout *layout = new QGridLayout(this);
        this->setLayout(layout);
        
        /// Add title and description
        {
            QLabel* ql = new QLabel("<i>"+filter->description()+"</i>",this);
            ql->setTextFormat(Qt::RichText);
            ql->setWordWrap(true);
            /// this widgets spans over two columns
            layout->addWidget(ql,0,0,1,2,Qt::AlignTop);
        }
        
        /// Adds parameters GUIs
        {
            parFrame = new ParametersFrame(this);
            parameters->setParent(parFrame);
            parFrame->loadFrameContent(parameters);
            layout->addWidget(parFrame,1,0,1,2);
        }

        /// Adds buttons & listeners        
        {
            /// the row where the line of buttons start
            int buttonRow = 2; 
            QPushButton *helpButton = new QPushButton("Help", this);
            QPushButton *closeButton = new QPushButton("Close", this);
            QPushButton *applyButton = new QPushButton("Apply", this);
            QPushButton *defaultButton = new QPushButton("Default", this);
            
            /// This allows just to press enter!
            applyButton->setDefault(true);
            
            #ifdef Q_WS_MAC
                // Hack needed on mac for correct sizes of button in the bottom of the dialog.
                helpButton->setMinimumSize(100, 25);
                closeButton->setMinimumSize(100,25);
                applyButton->setMinimumSize(100,25);
                defaultButton->setMinimumSize(100, 25);
            #endif
             
            /// @todo re-enable dynamic behavior            
            // if(filter->isDynamic()) {
                // previewCB = new QCheckBox("Preview", qf);
                // previewCB->setCheckState(Qt::Unchecked);
                // gridLayout->addWidget(previewCB,    buttonRow+0,0,Qt::AlignBottom);
                // connect(previewCB,SIGNAL(toggled(bool)),this,SLOT( togglePreview() ));
                // buttonRow++;
            // }
            
            layout->addWidget(helpButton,   buttonRow+0,1,Qt::AlignBottom);
            layout->addWidget(defaultButton,buttonRow+0,0,Qt::AlignBottom);
            layout->addWidget(closeButton,  buttonRow+1,0,Qt::AlignBottom);
            layout->addWidget(applyButton,  buttonRow+1,1,Qt::AlignBottom);
            
            connect(helpButton,SIGNAL(clicked()),this,SLOT(toggledButton_Help()));
            connect(applyButton,SIGNAL(clicked()),this,SLOT(toggledButton_Apply()));
            connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));
            connect(defaultButton,SIGNAL(clicked()),this,SLOT(toggledButton_Reset()));
        }
        
        this->showNormal();
        this->adjustSize();
    }
    
    /// @todo re-enable dynamic behavior
    // if(filter->isDynamic()) {
        //meshState.create(curmask, curModel);
        //connect(stdParFrame,SIGNAL(dynamicFloatChanged(int)), this, SLOT(applyDynamic()));
        //connect(stdParFrame,SIGNAL(parameterChanged()), this, SLOT(applyDynamic()));
    // }
    
    /// @todo What is this for?
    /// @answer to make sure you re-load the filter GUI when you click on another model
    // connect(curMeshDoc, SIGNAL(currentMeshChanged(int)),this, SLOT(changeCurrentMesh(int)));
}

void FilterDialog::toggledButton_Help(){
    QMessageBox::warning(this, "Unknown type", "toggledButton_Help() not supported!");
}

void FilterDialog::toggledButton_Apply(){
    /// Read the user edited values
    parFrame->readValues();
    emit requestedExectuteFilter(filter, parameters);
}

void FilterDialog::toggledButton_Reset(){
	QMessageBox::warning(this, "Unknown type", "toggledButton_Help() not supported!");
}
